#include <glib.h>

#define TEST_ARRAY_LEN 10

static void _test_hash_str_foreach_func(gpointer key, gpointer value, gpointer user_data)
{
    g_print("%s: key:%s, value:%s \n", (gchar *)user_data, (gchar *)key, (gchar *)value);
    return;
}

static void test_hash_basic(void)
{
    gint i = 0;
    char *arr[TEST_ARRAY_LEN] = {NULL};
    GHashTable *htable = NULL;

    g_print("\n");

    htable = g_hash_table_new(g_str_hash, g_str_equal);

    for(i=0; i<TEST_ARRAY_LEN/2; i++) {
        arr[i] = g_strdup_printf("str%d", i);
        g_hash_table_insert(htable, arr[i], arr[i]);
    }
    for(i=TEST_ARRAY_LEN/2;i<TEST_ARRAY_LEN;i++) {
        arr[i] = g_strdup_printf("str%d", i);
        g_hash_table_add(htable, arr[i]);
    }

    g_hash_table_foreach(htable, _test_hash_str_foreach_func, "str");

    g_print("htable length: %d \n", g_hash_table_size(htable));

    g_hash_table_destroy(htable);

    for(i=0; i<TEST_ARRAY_LEN; i++) {
        g_free(arr[i]);
    }

    return;
}

gint main(gint argc, gchar **argv)
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func ("/hash/basic", test_hash_basic);

    return g_test_run();
}